﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace DongleTest
{
    class GAP
    {
        /// <summary>
        /// 设备发现请求
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GAP_DeviceDiscoveryRequest
        {
            public byte Type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte DataLength;
            public byte Mode;
            public byte ActiveScan;
            public byte WhiteList;
        }

        /// <summary>
        /// 人机交互延伸命令状态
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GAP_HCI_ExtentionCommandStatus
        {
            public byte Type;
            public byte EventCode;
            public byte DataLength1;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Event;
            public byte Status;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte DataLength2;
        }

        /// <summary>
        /// 设备信息
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GAP_DeviceInformation
        {
            public byte Type;
            public byte EventCode;
            public byte DataLength1;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Event;
            public byte Status;
            public byte EventType;
            public byte AddrType;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
            public byte[] Addr;
            public sbyte Rssi;
            public byte DataLength2;
        }

        /// <summary>
        /// 设备初始化
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GAP_DeviceInit
        {
            public byte Type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte DataLength;
            public byte ProfileRole;
            public sbyte MaxScanRsps;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
            public byte[] IRK;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
            public byte[] CSRK;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
            public byte[] SignCounter;
        }

        /// <summary>
        /// 设备初始化完毕
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GAP_DeviceInitDone
        {
            public byte Type;
            public byte EventCode;
            public byte DataLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Event;
            public byte Status;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
            public byte[] DevAddr;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] DataPktLen;
            public byte NumDataPkts;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
            public byte[] IRK;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
            public byte[] CSRK;
        }

        /// <summary>
        /// 获取参数
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GAP_GetParam
        {
            public byte Type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte DataLength;
            public byte ParamID;
            // 0x15 (21)  最小链路层连接间隔，使用连接建立过程时（毫秒）。
            // 0x16 (22)  最大链路层连接间隔，使用连接建立时proc（毫秒）。
            // 0x1A (26)  链路层连接从属延迟，当使用连接建立过程（毫秒）
            // 0x19 (25)  链路层连接监视超时，使用连接建立时proc（毫秒）。

        }

        /// <summary>
        /// 获取参数
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GAP_Read
        {
            public byte Type;
            public byte EventCode;
            public byte DataLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Event;
            public byte Status;
        }

        /// <summary>
        /// 连接到设备请求
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GAP_EstablishLinkRequest
        {
            public byte Type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte DataLength;
            public byte HighDutyCycle;
            public byte WhiteList;
            public byte AddrTypePeer;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
            public byte[] PeerAddr;
        }

        /// <summary>
        /// 检索出所有特性
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GATT_DiscAllCharDescs
        {
            public byte Type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte DataLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] ConnHandle;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] StartHandle;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] EndHandle;

        }

        /// <summary>
        /// 检索出所有特性
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct ATT_FindInfoRsp
        {
            public byte Type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] EventCode;
            public byte DataLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Event;
            public byte Status;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] ConnHandle;
            public byte PduLen;
            public byte Format;
        }

        /// <summary>
        /// 特征值读取
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GATT_WriteCharValueFFF1
        {
            public byte Type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte DataLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] ConnHandle;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Handle;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public byte[] Value;
        }

        /// <summary>
        /// 特征值读取
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GATT_WriteCharValueFFF3
        {
            public byte Type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte DataLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] ConnHandle;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Handle;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
            public byte[] Value;
        }

        /// <summary>
        /// 特征值读取
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GATT_WriteCharValueFFF2
        {
            public byte Type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte DataLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] ConnHandle;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Handle;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Value;
        }

        /// <summary>
        /// 终止连接请求
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GAP_TerminateLinkRequest
        {
            public byte Type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte DataLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] ConnHandle;
            public byte DiscReason;
        }

        /// <summary>
        /// 测试结果返回
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct ATT_HandleValueNotification
        {
            public byte Type;
            public byte EventCode;
            public byte DataLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Event;
            public byte Status;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] ConnHandle;
            public byte PduLen;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Handle;
        }


        /// <summary>
        /// 特性写入应答
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct ATT_WriteRsp
        {
            public byte Type;
            public byte EventCode;
            public byte DataLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Event;
            public byte Status;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] ConnHandle;
            public byte PduLen;
        }


        /// <summary>
        /// 获取RSSI
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct HCI_ReadRSSI
        {
            public byte Type;
            public byte EventCode;
            public byte DataLength;
            public byte Packets;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte Status;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Handle;
            public sbyte RSSI;
        }

        /// <summary>
        /// 特征值写入
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GATT_ReadCharDesc
        {
            public byte Type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte DataLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] ConnHandle;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Handle;
        }

        /// <summary>
        /// 读取应答
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct ATT_ReadRsp
        {
            public byte Type;
            public byte EventCode;
            public byte DataLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] Event;
            public byte Status;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] ConnHandle;
            public byte PduLen;
        }

        /// <summary>
        /// 获取RSSI请求
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct HCI_ReadRSSI_RSP
        {
            public byte Type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte DataLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] ConnHandle;
        }

        /// <summary>
        /// 蓝牙搜索取消
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct GAP_DeviceDiscoveryCancel
        {
            public byte Type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] OpCode;
            public byte DataLength;
        }
    }
}
